<template>
  <div>
    <div class="i-table-no-border">
      <div>
        <table-btns
          :tableTopOperateData="listData.tableTopOperate"
          :tableTopBatchOperate="listData.tableTopBatchOperate"
          :selectedData="selectedData"
          :requestApi="listData.requestApi"
          @pSub="pSub"
          :basePath="basePath"
          @exportData="exportData"
          @actionsChange="getData"
        >
        </table-btns>
      </div>
      <!-- 表格内容 start  -->
      <Table
        ref="table"
        :columns="columns"
        :data="tableDataList"
        :loading="loading"
        class="ivu-mt"
        stripe
        @on-filter-change="handleFilterChange"
        @on-select="handleSelect"
        @on-select-cancel="handleSelectCancel"
        @on-select-all="handleSelectAll"
        @on-select-all-cancel="handleSelectAllCancel"
      >
        <template slot-scope="{ row, index }" slot="productName">
          <span>{{ row.old.productName }}</span>
        </template>
        <template slot-scope="{ row, index }" slot="spec">
          <span>{{ row.old.spec }}</span>
        </template>
        <template slot-scope="{ row, index }" slot="productManagerArea">
          <span>{{ row.old.productManagerArea }}</span>
        </template>
        <template slot-scope="{ row, index }" slot="storeNoUpdate">
          <span>{{ row.old.storeNo }}</span>
        </template>
        <template slot-scope="{ row, index }" slot="storeNameUpdate">
          <span>{{ row.old.storeName }}</span>
        </template>
        <template slot-scope="{ row, index }" slot="unit">
          <span>{{ row.old.unit }}</span>
        </template>
        <template slot-scope="{ row, index }" slot="compareArea">
          <span>{{ row.old.compareArea }}</span>
        </template>
        <template slot-scope="{ row, index }" slot="compareAreaUpdate">
          <Input
            v-model="row.old.compareArea"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </template>
        <template slot-scope="{ row, index }" slot="compareType">
          <span>{{ row.old.compareType }}</span>
        </template>
        <template slot-scope="{ row, index }" slot="compareTypeUpdate">
          <Select
            v-model="row.old.compareType"
            style="width: 120px"
            @on-change="compareTypeSelect($event, row)"
          >
            <Option
              v-for="item in compareTypeUpdateList"
              :value="item.optionValue"
              :key="item.optionHtml"
            >
              {{ item.optionHtml }}
            </Option>
          </Select>
        </template>
        <template slot-scope="{ row, index }" slot="compareTarget">
          <span>{{ row.old.compareTarget }}</span>
        </template>
        <template slot-scope="{ row, index }" slot="compareTargetUpdate">
          <div v-if="row.old.compareType === '2'">
            <Input
              v-model="row.old.compareTarget"
              @on-change="changInput(row, index)"
              placeholder="请输入..."
            />
          </div>
          <div v-else>
            <Input
              v-model="row.old.compareTarget"
              placeholder="请选择"
              @on-search="compareFun(row, index)"
              search
              enter-button
              readonly
              style="width: 120px"
            />
          </div>
        </template>
        <template slot-scope="{ row, index }" slot="compareTelUpdate">
          <div v-if="row.old.compareType === '2'">
            <Input
              v-model="row.old.compareTel"
              @on-change="changInput(row, index)"
              placeholder="请输入..."
            />
          </div>
          <div v-else>
            <Input
              v-model="row.old.compareTel"
              disabled
              @on-change="changInput(row, index)"
              placeholder="请输入..."
            />
          </div>
        </template>
        <template slot-scope="{ row, index }" slot="compareAddressUpdate">
          <div v-if="row.old.compareType === '2'">
            <Input
              v-model="row.old.compareAddress"
              @on-change="changInput(row, index)"
              placeholder="请输入..."
            />
          </div>
          <div v-else>
            <Input
              v-model="row.old.compareAddress"
              disabled
              @on-change="changInput(row, index)"
              placeholder="请输入..."
            />
          </div>
        </template>
        <template slot-scope="{ row, index }" slot="lastSaleCountUpdate">
          <Input
            v-model="row.lastSaleCountUpdate"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </template>
        <template slot-scope="{ row, index }" slot="nowSaleCountUpdate">
          <Input
            v-model="row.nowSaleCountUpdate"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </template>
        <template slot-scope="{ row, index }" slot="brandDiffUpdate">
          <Input
            v-model="row.old.brandDiff"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </template>
        <template slot-scope="{ row, index }" slot="saleRateUpdate">
          <Input
            v-model="row.old.saleRate"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </template>
        <template slot-scope="{ row, index }" slot="compareRuleUpdate">
          <Input
            v-model="row.old.compareRule"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </template>
        <template slot-scope="{ row, index }" slot="purchaseRateMaxUpdate">
          <Input
            v-model="row.old.purchaseRateMax"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </template>
        <template slot-scope="{ row, index }" slot="purchaseRateMinUpdate">
          <Input
            v-model="row.old.purchaseRateMin"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </template>
        <template slot-scope="{ row, index }" slot="purchasePriceMaxUpdate">
          <Input
            v-model="row.old.purchasePriceMax"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </template>
        <template slot-scope="{ row, index }" slot="purchasePriceMinUpdate">
          <Input
            v-model="row.old.purchasePriceMin"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </template>
        <template slot-scope="{ row, index }" slot="action">
          <div v-if="row.actions && row.actions.length > 2">
            <a @click="handleClickMore(row.actions[0].name, row)">{{
              row.actions ? row.actions[0].label : ""
            }}</a>
            <Divider type="vertical" />
            <Dropdown @on-click="(name) => handleClickMore(name, row)" transfer>
              <a
                >更多
                <Icon type="ios-arrow-down" />
              </a>
              <DropdownMenu slot="list">
                <DropdownItem
                  v-for="(item, i) in row.actions.slice(1)"
                  :key="i"
                  :name="item.name"
                  >{{ item.label }}
                </DropdownItem>
              </DropdownMenu>
            </Dropdown>
          </div>
          <div v-if="row.actions && row.actions.length <= 2">
            <a
              v-if="row.actions"
              @click="handleClickMore(row.actions[0].name, row)"
              >{{ row.actions[0].label }}</a
            >
            <Divider
              v-if="row.actions && row.actions.length > 1"
              type="vertical"
            />
            <a
              v-if="row.actions && row.actions.length > 1"
              @click="handleClickMore(row.actions[1].name, row)"
              >{{ row.actions[1].label }}</a
            >
          </div>
        </template>
      </Table>
      <!-- 表格内容 end  -->
    </div>
    <Modal v-model="compareModal" title="选择比价对象" width="800">
      <Row type="flex"> </Row>
      <Row style="margin-top: 15px">
        <Table :columns="compareCol" :data="compareData">
          <template slot-scope="{ row, index }" slot="action">
            <Button
              type="success"
              size="small"
              style="margin: 3px 0"
              @click="compareOk(row)"
              >选择</Button
            >
          </template>
        </Table>
        <div class="ivu-mt ivu-text-center">
          <Page
            :total="totalNum"
            show-total
            show-sizer
            :current.sync="nowPage"
            @on-change="linkAttributeChang()"
            @on-page-size-change="handleChangePage"
          />
        </div>
      </Row>
    </Modal>
  </div>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
import util from '@/libs/util';
import { ApiRequest } from '@api/basePage';
import tables from '@/mixins/dynamictable'; // 混入通用代码

import qwInput from '@/components/common/formItem/qwInput'; // 表单input组件
import qwSelect from '@/components/common/formItem/qwSelect'; // 表单select组件
import qwDatePicker from '@/components/common/formItem/qwDatePicker'; // 表单日期组件
import qwSelectTree from '@/components/common/formItem/qwSelectTree'; // 表单下拉树组件
import tableBtns from './tableBtns'; // 表格内部操作按钮

export default {
  mixins: [tables],
  props: {
    dynamicData: {
      type: Object
    },
    pagePath: {
      type: String
    },
    orderState: {
      type: String,
      default: ''
    }
  },
  computed: {
    ...mapState('admin/baseList', [
      'listData',
      'searchFormData',
      'searchFormDataQ'
    ])
  },
  watch: {
    orderState (n, o) {
      this.getData();
    }
  },
  components: {
    qwInput,
    qwSelect,
    qwDatePicker,
    qwSelectTree,
    // tableActions,
    tableBtns
  },
  data () {
    return {
      actions: [],
      specTableData: [],
      util: util,
      basePath: this.dynamicData.basePath, // 拼接基础路径
      compareModal: false,
      tableDataList: [],
      compareDate: [],
      selectIndex: 0,
      compareCol: [
        {
          title: '比价对象',
          key: 'compareTargetUpdate',
          align: 'center'
        },
        {
          title: '比价对象名字',
          key: 'compareTargetNameUpdate',
          align: 'center'
        },
        {
          title: '联系方式',
          key: 'compareTelUpdate',
          align: 'center'
        },
        {
          title: '地址',
          key: 'compareAddressUpdate',
          align: 'center'
        },
        {
          title: '操作',
          slot: 'action',
          align: 'center',
          fixed: 'right',
          minWidth: 120
        }
      ],
      compareTypeUpdateList: [
        {
          optionValue: '1',
          optionHtml: '一品多供'
        },
        {
          optionValue: '2',
          optionHtml: '竞争对手'
        },
        {
          optionValue: '3',
          optionHtml: '同品类供应商'
        },
        {
          optionValue: '4',
          optionHtml: '同品牌供应商'
        }
      ]
    };
  },
  created () {},
  methods: {
    ...mapMutations('admin/baseList', ['setTableRowData']),
    compareFun (row, index) {
      this.compareModal = true;
      this.selectIndex = index;
    },
    // 选中后处理
    compareOk (row) {
      this.tableDataList[this.selectIndex].compareTargetUpdate =
        row.compareTargetNameUpdate;
      this.tableDataList[this.selectIndex].compareTelUpdate =
        row.compareTelUpdate;
      this.tableDataList[this.selectIndex].compareAddressUpdate =
        row.compareAddressUpdate;
      this.tableDataList[this.selectIndex].compareTargetNameUpdate =
        row.compareTargetNameUpdate;
      this.tableDataList[this.selectIndex].compareManUpdate =
        row.compareManUpdate;
      this.compareModal = false;
    },
    // 获取表格的数据
    getData () {
      let searchParam = [];
      Object.keys(this.searchFormData).forEach((key) => {
        searchParam.push({
          name: key,
          value: this.searchFormData[key]
        });
      });
      Object.keys(this.searchFormDataQ).forEach((key) => {
        searchParam.push({
          name: key,
          value: this.searchFormDataQ[key]
        });
      });
      let data = {
        nowPage: this.nowPage,
        pageShow: this.pageShow,
        searchParam: JSON.stringify(searchParam),
        sortName: '',
        sortType: ''
      };

      // 开始请求列表
      let listArr = [];
      listArr[0] = this.$route.query.list;
      listArr[0].compareAreaUpdate = '';
      listArr[0].compareTypeUpdate = '';
      listArr[0].compareTargetUpdate = '';
      listArr[0].compareTelUpdate = '';
      listArr[0].compareAddressUpdate = '';
      listArr[0].brandDiffUpdate = '';
      listArr[0].lastSaleCountUpdate = '';
      listArr[0].nowSaleCountUpdate = '';
      listArr[0].saleRateUpdate = '';
      listArr[0].compareRuleUpdate = '';
      listArr[0].purchaseRateMaxUpdate = '';
      listArr[0].purchaseRateMinUpdate = '';
      listArr[0].purchasePriceMaxUpdate = '';
      listArr[0].purchasePriceMinUpdate = '';
      let tableDataList = listArr;
      tableDataList.forEach((item) => {
        let actions = [];
        actions.push({
          name: 'Sub',
          label: '提交',
          handleType: 'prop'
        });
        item.actions = actions;
      });
      // 增加表格操作项
      this.tableDataList = listArr;
    },
    compareTypeSelect (type, row) {
      let data = {};
      ApiRequest(
        '/sysback/purchasepriceruleupdate/getRuleTargetByType?' +
          'areaCode=' +
          row.areaCode +
          '&skuNo=' +
          row.skuNo +
          '&supplierName=' +
          row.supplierName +
          '&supplierType=' +
          row.supplierType +
          '&type=' +
          type,
        'get',
        data
      ).then((res) => {
        if (res.retStatus === '1') {
          this.compareData = res.retData;
        }
      });
    },
    changInput (row, index) {
      this.tableDataList[index] = row;
    },
    // 批量提交按钮
    pSub () {
      if (this.tableDataList.length > 0) {
        ApiRequest(
          '/sysback/purchasepriceruleupdate/batchUpdateRule',
          'post',
          this.tableDataList
        ).then((res) => {
          if (res.retStatus == '1') {
            this.$Message.success('提交成功');
            this.getData();
          }
        });
      }
    },
    handleClickMore (name, row) {
      if (name === 'Delete') {
        this.$Modal.confirm({
          title: '删除操作',
          content: '确定删除此数据吗？',
          onOk: () => {
            let data = {
              uuid: row.uuid
            };
            // 开始请求商品列表
            ApiRequest(
              this.requestApi.delTableList.url,
              this.requestApi.delTableList.type,
              data
            ).then((res) => {
              this.$Message.success('删除成功');
              this.actionsChange();
            });
          }
        });
      } else {
        let obj = {}; // 动作的对象信息
        row.actions.forEach((item) => {
          if (name === item.name) {
            obj = item;
            return false;
          }
        });
        this.setTableRowData(row);
        if (obj.handleType === 'link') {
          // 操作类型为链接时，拼接参数跳转
          let jumpPath = this.basePath + obj.name;
          this.$router.push({
            path: jumpPath,
            query: {
              uuid: row.uuid,
              orderType: row.orderType
            }
          });
        } else if (obj.handleType === 'prop') {
          // 操作类型为弹窗时 打开弹窗
          if (obj.name === 'viewing') {
            this.showPropviewing = true;
          } else if (obj.name === 'view') {
            this.showPropviewing = true;
          } else if (obj.name === 'Sub') {
            let datas = {
              oldUuid: row.old.uuid,
              skuNo: row.skuNo,
              spuNo: row.spuNo,
              productName: row.old.productName,
              productManagerArea: row.old.productManagerArea,
              productUuid: row.old.productUuid,
              unit: row.old.unit,
              spec: row.old.spec,
              storeNameUpdate: row.old.storeName,
              storeUuidUpdate: row.old.storeUuid,
              storeNoUpdate: row.old.storeNo,
              compareAreaUpdate: row.old.compareArea,
              compareTypeUpdate: row.old.compareType,
              compareTargetNameUpdate: row.old.compareTargetName,
              compareTargetUpdate: row.old.compareTarget,
              compareTelUpdate: row.old.compareTel,
              compareManUpdate: row.old.compareMan,
              compareAddressUpdate: row.old.compareAddress,
              brandDiffUpdate: row.old.brandDiff,
              lastSaleCountUpdate: row.old.lastSaleCount,
              nowSaleCountUpdate: row.old.nowSaleCount,
              saleRateUpdate: row.old.saleRate,
              compareRuleUpdate: row.old.compareRule,
              purchaseRateMaxUpdate: row.old.purchaseRateMax,
              purchaseRateMinUpdate: row.old.purchaseRateMin,
              purchasePriceMaxUpdate: row.old.purchasePriceMax,
              purchasePriceMinUpdate: row.old.purchasePriceMin
            };
            ApiRequest(
              this.listData.requestApi.addRule.url,
              this.listData.requestApi.addRule.type,
              datas
            ).then((res) => {
              if (res.retStatus == '1') {
                this.$Message.success('操作成功！');
                let jumpPath = this.basePath + 'Startuse';
                this.$router.push({ path: jumpPath });
              }
            });
          } else {
          }
        } else {
          // 操作类型为弹窗时 打开弹窗
        }
      }
    }
  }
};
</script>
<style lang="less" scoped>
.i-table-no-border .ivu-table-header th {
  background: #f8f8f8;
}
</style>
